package com.itheima.chapter09.file;

import java.io.File;
import java.util.HashMap;

//统计一个文件夹中，每种文件出现的次数
public class FileCase3 {
    public static void main(String[] args) {
        File file =new File("MyIdeaTest");
        HashMap<String,Integer> hm =new HashMap<>();
        getCount(hm,file);
        System.out.println(hm);
    }
    private static void getCount(HashMap<String,Integer> hm,File file){
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()){
                String filename = f.getName();
                String[] fileNameArr = filename.split("\\.");
                if (fileNameArr.length==2){
                    String fileEndname =fileNameArr[1];
                    if (hm.containsKey(fileEndname)){
                        Integer count = hm.get(fileEndname);
                        count++;
                        hm.put(fileEndname,count);
                    }else {
                        hm.put(fileEndname,1);
                    }
                }
            }else {
                getCount(hm,f);
            }
        }

    }
}
